// packageE/invoice/invoice.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    activeTab: 0,
    activeColor: '#fe2721',
    orderList:[],
    orderHistoryList:[],
    checkAll:false,
    page:1,
    total:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getList();
    // this.getHistoryList();
  },
  onClickTab(e) {
    // const { index: itemIndex, title } = e.detail;
    // this.setData({
    //   activeTab: tabIndex,
    // });
  },
  changeSelect(e){
    let index = e.currentTarget.dataset.itemindex;
    this.data.orderList[index].checked = !this.data.orderList[index].checked;
    this.setData({
      orderList:this.data.orderList
    })
    let isCheckAll = this.data.orderList.every(item=>{
      return item.checked === true
    })
    this.setData({
      checkAll: isCheckAll,
    });
    
  },
   //全选
   allSelect(event) {
    let bol = event.detail;
    this.setData({
      checkAll: bol,
    });
    this.data.orderList.forEach(item=>{
      item.checked = bol
    })
    this.setData({
      orderList:this.data.orderList
    })
  },
  toAdd(){
    let ids = []
     this.data.orderList.forEach(item=>{
       if(item.checked === true){
        ids.push(item.id)
       }
    })
    if(ids.length === 0){
      wx.showToast({
        title: '请选择订单',
        icon: "none",
        duration: 1000,
      });
      return false
    }
    wx.navigateTo({
      url: `/packageE/invoice/addInvoice/addInvoice?ids=${ids.join(',')}`,
    })
  },
  getHistoryList(){
    
    var that = this;
    let urlStr = app.getNetAddresss(`invoice.invoice.apply-logs&page=${this.data.page}`);
    app._postNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            orderHistoryList:[...that.data.orderHistoryList,...res.data.data],
            total:res.data.total
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  getList() {
    var that = this;
    let urlStr = app.getNetAddresss(`invoice.invoice.order-list&page=${this.data.page}`);
    app._postNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          res.data.data.forEach(item=>{
            item['checked'] = false
          })
          //that.data.orderList.concat(res.data.data)
          //console.log(that.data.orderList)
          that.setData({
            orderList:[...that.data.orderList,...res.data.data],
            total:res.data.total
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { 
    this.setData({
      orderList:[],
      orderHistoryList:[]
    })
    this.getList();
    this.getHistoryList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () { 
    if(this.data.orderList < this.data.total){
      this.setData({
        page:this.data.page+1
      })
      this.getList();
    }
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () { },
});
